package com.jieshao.enhance

import com.intellij.openapi.editor.Editor
import git4idea.GitUtil
import git4idea.repo.GitRepository

val Editor.lineSelection: Pair<Int, Int>
    get() {
        val caretStates = caretModel.caretsAndSelections

        if (caretStates.size < 1) {
            val singleLine = caretModel.logicalPosition.line + 1
            return singleLine to singleLine
        }

        val caretState = caretStates[0]

        val start = caretState.selectionStart
        val end = caretState.selectionEnd

        if (start == null || end == null) {
            val singleLine = caretModel.logicalPosition.line + 1
            return singleLine to singleLine
        }

        return start.line + 1 to end.line + 1
    }

fun GitRepository.locateRemote(name: String) = GitUtil.findRemoteByName(this, name)
